package com.wuseguang.springcloud.controller;

import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.Holiday;
import com.wuseguang.springcloud.entity.HolidayList;
import com.wuseguang.springcloud.service.BanCardService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @描述 设备管理-->展示设备信息
 * @创建人 zm
 * @创建时间 2019/12/26
 */
@RestController
@RequestMapping("/bancard")
public class BanCardController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BanCardService banCardService;
    //推送接口
    @Autowired
    private ClassCardService classCardService;

    /**
     * @描述 组合查询设备详情
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/queryBanCardAll")
    public Result queryBanCardAll(RequestParameterWrapper request,
                                  @RequestParam(value = "gradeid", required = false) String gradeid,//年级id
                                  @RequestParam(value = "classid", required = false) String classid,//班级id
                                  @RequestParam(value = "state", required = false) String state,//0是离线  1是在线
                                  @RequestParam(value = "deviceid", required = false) String deviceid) {//设备号 mac地址
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},gradeid={},classid={},state={},deviceid={}", schoolId, gradeid, classid, state, deviceid);
        return new Result(banCardService.queryBanCardAll(schoolId, gradeid, classid, state, deviceid));
    }

    /**
     * @描述 设备一键关机或批量一键关机
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updateBancardShutdown")
    public Result updateBancardShutdown(RequestParameterWrapper request,
                                        @RequestParam(value = "bancardids") String bancardids) {//班牌id 格式 "1,2,3"
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={}", schoolId, bancardids);
        int i = banCardService.updateDeviceShutdown(schoolId, bancardids);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 升级
     * @创建人 zm
     * @创建时间 2019/12/29
     */
    @RequestMapping("/updateUpgradeflag")
    public Result updateUpgradeflag(RequestParameterWrapper request,
                                    @RequestParam(value = "upgradeurl") String upgradeurl,
                                    @RequestParam(value = "bancardids") String bancardids) {//班牌id 格式 "1,2,3"
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={}", schoolId, bancardids);
        int i = banCardService.updateUpgradeflag(schoolId, upgradeurl, bancardids);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }


    /**
     * @描述 设备一键重启或批量一键重启
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updateBancardRestartflag")
    public Result updateBancardRestartflag(RequestParameterWrapper request,
                                           @RequestParam(value = "bancardids") String bancardids) {//班牌id 格式 "1,2,3"
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={}", schoolId, bancardids);
        int i = banCardService.updateDeviceRestartflag(schoolId, bancardids);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 设备一键截屏或批量一键截屏
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updateBancardScreenflag")
    public Result updateBancardScreenflag(RequestParameterWrapper request,
                                          @RequestParam(value = "bancardids") String bancardids) {//班牌id 格式 "1,2,3"
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={}", schoolId, bancardids);
        int i = banCardService.updateDeviceScreenflag(schoolId, bancardids);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 重置密码
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updatepassword")
    public Result updatepassword(RequestParameterWrapper request,
                                 @RequestParam(value = "password") String password) {//密码
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={},password={}", schoolId, password);
        int i = banCardService.updatepassword(schoolId, password);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 声音控制
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updateVolume")
    public Result updateVolume(RequestParameterWrapper request,
                               @RequestParam(value = "bancardids") String bancardids,//班牌id 格式 "1,2,3"
                               @RequestParam(value = "volume") String volume) {//音量大小
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={},volume={}", schoolId, volume);
        int i = banCardService.updateVolume(schoolId, bancardids, volume);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 删除设备，并删除相关节假日的信息
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/delBancard")
    public Result delBancard(RequestParameterWrapper request,
                             @RequestParam(value = "bancardids") String bancardids) {//班牌id 格式 "1,2,3"
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},bancardids={}", schoolId, bancardids);
        int i = banCardService.delDevice(schoolId, bancardids);
        if (i > 0) {
            return new Result();
        }
        return new Result(CodeUtil.DELETE_FAILED);
    }

    /**
     * @描述 查询所有设备类型
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/queryDeviceType")
    public Result queryDeviceType() {
        logger.info("queryDeviceType.....");
        return new Result(banCardService.queryDeviceType());
    }

    /**
     * @描述 查询所有教室
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/queryClassRoomAll")
    public Result queryClassRoomAll(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={}", schoolId);
        return new Result(banCardService.queryClassRoomAll(schoolId));
    }

    /**
     * @描述 查询该设备所有节假日时间
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/queryHolidayAll")
    public Result queryHolidayAll(@RequestParam(value = "bancardid", required = false) String bancardid) {
        logger.info("bancardid={}", bancardid);
        return new Result(banCardService.queryHolidayAll(bancardid));
    }

    /**
     * @描述 查询该设备使用的节假日时间
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/queryHolidayUse")
    public Result queryHolidayUse(@RequestParam(value = "bancardid") String bancardid) {
        logger.info("bancardid={}", bancardid);
        return new Result(banCardService.queryHolidayAllUse(bancardid));
    }

    /**
     * @描述 修改班牌日常时间
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updateBancardTime")
    public Result updateBancardTime(@RequestParam(value = "bancardids") String bancardids,//班牌id 格式 "1,2,3"
                                    @RequestParam(value = "monday", required = false) String monday,//周一到周日 时间格式 "08:00-19:00"
                                    @RequestParam(value = "tuesday", required = false) String tuesday,
                                    @RequestParam(value = "wednesday", required = false) String wednesday,
                                    @RequestParam(value = "thursday", required = false) String thursday,
                                    @RequestParam(value = "friday", required = false) String friday,
                                    @RequestParam(value = "saturday", required = false) String saturday,
                                    @RequestParam(value = "sunday", required = false) String sunday) {
        logger.info("bancardids={},monday={},tuesday={},wednesday={},thursday={},friday={},saturday={},sunday={}", bancardids, monday, tuesday, wednesday, thursday, friday, saturday, sunday);
        int i = banCardService.updateBancardTime(bancardids, monday, tuesday, wednesday, thursday, friday, saturday, sunday);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 批量修改节假日
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/batchAddHoliday")
    public Result batchAddHoliday(@RequestBody HolidayList holidayList) {
        if (holidayList == null || holidayList.getHolidayList() == null || holidayList.getBancardids() == null || holidayList.getBancardids().length == 0) {
            return new Result(CodeUtil.MODIFICATION_FAILED);
        }
        String[] bancardids = holidayList.getBancardids();
        List<Holiday> holidayList1 = holidayList.getHolidayList();
        logger.info("bancardids={}", bancardids);
        String s = "";
        for (int j = 0; j < bancardids.length; j++) {
            if (j == bancardids.length - 1) {
                s += bancardids[j];
            } else {
                s += bancardids[j] + ",";
            }
        }
        int i = banCardService.batchAddHoliday(s, bancardids, holidayList1);
        if (i > 0) {
            classCardService.bancardAndCards(s);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 修改设备类型和绑定的教室
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/updateDevicTypeorclassroom")
    public Result updateDevicTypeorclassroom(@RequestParam(value = "bancardid") String bancardid,//班牌id
                                             @RequestParam(value = "classroomid") String classroomid,//教室id
                                             @RequestParam(value = "deviceType") String deviceType,//设备类型
                                             @RequestParam(value = "deviceid") String deviceid) {//设备mac地址
        logger.info("bancardid={},classroomid={},deviceType={}", bancardid, classroomid, deviceType);
        Integer integer3 = banCardService.queryCount(deviceid, bancardid);
        if (integer3 > 0) {
            return new Result(CodeUtil.BEEN_BOUND);
        }
        int i = banCardService.updateDevicTypeorclassroom(bancardid, deviceType, classroomid, deviceid);
        if (i > 0) {
            classCardService.bancardAndCards(bancardid);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 批量修改设备类型
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    @RequestMapping("/batchupdateDevicType")
    public Result batchupdateDevicType(@RequestParam(value = "bancardids") String bancardids,//班牌id
                                       @RequestParam(value = "deviceType") String deviceType) {//设备类型
        if (deviceType != null && "深海瑞格".equals(deviceType.trim())) {
            deviceType = "ShenHaiRuiGe";
        }
        logger.info("bancardids={},deviceType={}", bancardids, deviceType);
        int i = banCardService.batchupdateDevicType(bancardids, deviceType);
        if (i > 0) {
            classCardService.bancardAndCards(bancardids);
            return new Result();
        }
        return new Result(CodeUtil.MODIFICATION_FAILED);
    }

    /**
     * @描述 绑定设备
     * @创建人 zm
     * @创建时间 2019/12/28
     */
    @RequestMapping("/addDevicTypeorclassroom")
    public Result addDevicTypeorclassroom(RequestParameterWrapper request,
                                          @RequestParam(value = "deviceid") String deviceid,
                                          @RequestParam(value = "classroomid") String classroomid,
                                          @RequestParam(value = "deviceType") String deviceType) {
        String schoolId = request.getParam("schoolId");
        logger.info("school_id={},deviceid={},classroomid={},deviceType={}", schoolId, deviceid, classroomid, deviceType);
        return banCardService.addDevicTypeorclassroom(schoolId, deviceid, deviceType, classroomid);
    }


    /**
     * @描述 根据班级id查设备id
     * @创建人 zm
     * @创建时间 2020/2/28
     */
    @RequestMapping("/queryDanCardIds")
    public Result queryDanCardIds(@RequestParam(value = "classids") String classids) {
        logger.info("queryDanCardIds-->classids={}", classids);
        return new Result(banCardService.queryDanCardIds(classids));
    }

    /**
     * @描述 查詢所有綁定了設備的班級
     * @创建人 zm
     * @创建时间 2020/3/10
     */
    @RequestMapping("/queryEquipmentClassesAll")
    public Result queryEquipmentClassesAll(RequestParameterWrapper request) {
        String schoolId = request.getParam("schoolId");
        return banCardService.queryEquipmentClassesAll(schoolId);
    }

    /**
     * @描述 获取版本号
     * @创建人 zm
     * @创建时间 2020/4/1
     */
    @PostMapping("/getVersionNumber")
    public Result getVersionNumber(@RequestPart(value = "file") MultipartFile file, @RequestParam(value = "bancardids") String bancardids) {
        return banCardService.getVersionNumber(file, bancardids);
    }
}
